package com.hilaver.dzmis.service.impl;

import java.util.List;
import java.util.Locale;

import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.basicinfo.BiFranceCustomer;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.dao.ProductIdentificationDAO;
import com.hilaver.dzmis.order.OrderProduct;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.service.BasePDFService;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.vo.OrderStatistics;
import com.hilaver.dzmis.vo.OrderStatisticsCriteria;
import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfPTable;

public class PDFOrderStatisticsExportServiceImpl extends BasePDFService{

	public static float CELL_HEIGHT = 16f;

	private ProductIdentificationDAO dao;

	private CtServiceImpl ctService;

	private Document document;
	
	private OrderProductServiceImpl orderProductServiceImpl;

	public PDFOrderStatisticsExportServiceImpl() {
	}

	public PDFOrderStatisticsExportServiceImpl(Document document, Locale locale) {
		super.locale = locale;
		this.dao = new ProductIdentificationDAO();
		this.orderProductServiceImpl = new OrderProductServiceImpl();
		this.ctService = new CtServiceImpl();
		super.i18n = new I18nServiceImpl();
		this.document = document;
		this.setPageConfig();
	}

	public void exportOrderStatistics(OrderStatisticsCriteria osc) throws Exception {
		List<OrderStatistics> osList = this.orderProductServiceImpl.doStatisticsList(osc);
		
		float[] widthsTitle = {0.6f, 0.8f, 0.8f, 0.9f, 0.6f, 0.5f, 0.8f, 1f, 1f, 0.6f, 1.2f};
		PdfPTable table = new PdfPTable(widthsTitle);
		table.addCell(super.getTableCell(getTableTitle(osc, this.locale), true, widthsTitle.length));
		
		table.addCell(getTableCell("ui.productidentification.seasonnumber", true, 1));
		table.addCell(getTableCell("message.edit.customername", true, 1));
		table.addCell(getTableCell("ui.francecustomer.department", true, 1));
		table.addCell(getTableCell("ui.productidentification.chart", true, 1));
		table.addCell(getTableCell("ui.productidentification.ordernumber", true, 1));
		table.addCell(getTableCell("ui.dzcolor.variant", true, 1));
		
		if("01".equals(osc.getIsTrimmingNeeded())){
			table.addCell(getTableCell("ui.dzcolor.customercolorcode", true, 1));
			table.addCell(getTableCell("ui.order.trimmingreference", true, 1));
			table.addCell(getTableCell("ui.order.trimmingcustomerreference", true, 1));
			table.addCell(getTableCell("ui.order.isyarnneeded", true, 1));
			table.addCell(getTableCell("message.edit.buyer", true, 1));
		}else{
			table.addCell(getTableCell("ui.yarn.reference", true, 1));
			table.addCell(getTableCell("ui.yarn.yarnaccount", true, 1));
			table.addCell(getTableCell("ui.dzcolor.code", true, 1));
			table.addCell(getTableCell("ui.dzcolor.customercolorcode", true, 1));
			table.addCell(getTableCell(new String[]{"ui.order.isyarnneeded","(Kg)"}, true, 1));
		}
		
		String dzColorCode = null;
		String trimmingDZReference = null;
		
		String groupStr = null;
		String preGroupStr = null;
		String groupKey = null;
		Float total = 0f;
		for (int i = 0; i < osList.size(); i++) {
			OrderStatistics os = osList.get(i);
			if("01".equals(osc.getIsYarnNeeded())){
				dzColorCode = os.getDzColorCode();
				groupStr = dzColorCode;
				groupKey = "ui.dzcolor.code";
			}else{
				trimmingDZReference = os.getTrimmingDZReference();
				groupStr = trimmingDZReference;
				groupKey = "ui.order.trimmingreference";
			}
			if(preGroupStr == null){
				preGroupStr = groupStr;
				table.addCell(super.getTableCell(new String[]{groupKey, ": " + groupStr}, true, widthsTitle.length, Paragraph.ALIGN_LEFT));
			}
			if(!preGroupStr.equals(groupStr)){
				if("01".equals(osc.getIsYarnNeeded())){
					table.addCell(super.getTableCell("", false, widthsTitle.length - 2));
					table.addCell(super.getTableCell(new String[]{"message.list.total",":"}, false, 1, Paragraph.ALIGN_RIGHT));
					table.addCell(super.getTableCell(StringUtils.toString(total, 3), false, 1, Paragraph.ALIGN_RIGHT));
				}else{
					table.addCell(super.getTableCell("", false, widthsTitle.length - 3));
					table.addCell(super.getTableCell(new String[]{"message.list.total",":"}, false, 1, Paragraph.ALIGN_RIGHT));
					table.addCell(super.getTableCell(StringUtils.toString(total, 0), false, 1, Paragraph.ALIGN_RIGHT));
					table.addCell(super.getTableCell("", false, 1));
				}
				
				total = 0f;
				table.addCell(super.getTableCell(new String[]{groupKey, ": " + groupStr}, true, widthsTitle.length, Paragraph.ALIGN_LEFT));
			}
			
			table.addCell(super.getTableCell(os.getCtSeasonNumber(), true, 1));
			table.addCell(super.getTableCell(os.getCustomerName(), true, 1));
			table.addCell(super.getTableCell(os.getCustomerDepartment(), true, 1));
			table.addCell(super.getTableCell(os.getPiReference(), true, 1));
			table.addCell(super.getTableCell(os.getOrderNumber(), true, 1));
			table.addCell(super.getTableCell(os.getVariant(), true, 1));
			
			if("01".equals(osc.getIsYarnNeeded())){
				table.addCell(super.getTableCell(os.getYarnReference(), true, 1));
				table.addCell(super.getTableCell(os.getYarnAccount(), true, 1));
				table.addCell(super.getTableCell(os.getDzColorCode(), true, 1));
				table.addCell(super.getTableCell(os.getCustomerColorCode(), true, 1));
				table.addCell(super.getTableCell(StringUtils.toString(os.getYarnNeeded(), 3), true, 1,Paragraph.ALIGN_RIGHT));
				total += os.getYarnNeeded().floatValue();
			}else{
				table.addCell(super.getTableCell(os.getCustomerColorCode(), true, 1));
				table.addCell(super.getTableCell(os.getTrimmingDZReference(), true, 1));
				table.addCell(super.getTableCell(os.getTrimmingCustomerReference(), true, 1));
				table.addCell(super.getTableCell(os.getTrimmingNeeded().toString(), true, 1,Paragraph.ALIGN_RIGHT));
				total += os.getTrimmingNeeded();
				table.addCell(super.getTableCell(os.getCtSupplyType(), true, 1));
			}
			
			
			
			
			preGroupStr = groupStr;
			if(i == osList.size() - 1){
				if("01".equals(osc.getIsYarnNeeded())){
					table.addCell(super.getTableCell("", false, widthsTitle.length - 2));
					table.addCell(super.getTableCell(new String[]{"message.list.total",":"}, false, 1, Paragraph.ALIGN_RIGHT));
					table.addCell(super.getTableCell(StringUtils.toString(total, 3), false, 1, Paragraph.ALIGN_RIGHT));
				}else{
					table.addCell(super.getTableCell("", false, widthsTitle.length - 3));
					table.addCell(super.getTableCell(new String[]{"message.list.total",":"}, false, 1, Paragraph.ALIGN_RIGHT));
					table.addCell(super.getTableCell(StringUtils.toString(total, 0), false, 1, Paragraph.ALIGN_RIGHT));
					table.addCell(super.getTableCell("", false, 1));
				}
			}
		}
		
		this.document.add(table);
	}

	private void setPageConfig() {
		this.document.setPageSize(PageSize.A4.rotate());
		this.document.setMargins(10, 10, 35, 35);
		HeaderFooter header = new HeaderFooter(new Phrase("Design Zone"), false);
		header.setBorder(Rectangle.NO_BORDER);
		header.setAlignment(Paragraph.ALIGN_RIGHT);
		header.setRight(140);
		this.document.setHeader(header);
		this.selector = new FontSelector();
		// selector.addFont(FontFactory.getFont(FontFactory.COURIER, 10));
		selector.addFont(FontFactory.getFont("Arial", 8));
		selector.addFont(FontFactory.getFont("STSong-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED, 9));
	}
	
	private void setTableDefTrimming(PdfPTable table) throws Exception{
		
	}
	
	public static String getTableTitle(OrderStatisticsCriteria osc, Locale locale) {
		try{
			ProductIdentificationDAO dao = new ProductIdentificationDAO();
			CtServiceImpl ctService = new CtServiceImpl();
			I18nServiceImpl i18n = new I18nServiceImpl();
			StringBuffer sb = new StringBuffer();
			if(osc.getDate() != null){
				sb.append(StringUtils.toString(osc.getDate()) + "-");
			}
			if(osc.getCtSeasonNumber() != null){
				sb.append(ctService.getDescription(CtSeasonNumber.class.getName(), osc.getCtSeasonNumber(), locale) + "-");
			}
			if(osc.getCustomerId() != null){
				BiFranceCustomer customer = (BiFranceCustomer)dao.get(BiFranceCustomer.class.getName(), osc.getCustomerId());
				sb.append(customer.getName() + "-");
				sb.append(customer.getDepartment()!=null?(customer.getDepartment()+"-"):"");
			}
			if(osc.getPiId() != null){
				ProductIdentification pi = (ProductIdentification)dao.get(ProductIdentification.class.getName(), osc.getPiId());
				sb.append(pi.getReference() + "-");
			}
			if(osc.getYarnId() != null){
				BiYarn yarn = (BiYarn)dao.get(BiYarn.class.getName(), osc.getYarnId());
				sb.append(yarn.getReference() + "-");
			}
			if(osc.getDzColorId() != null){
				BiDZColor color = (BiDZColor)dao.get(BiDZColor.class.getName(), osc.getDzColorId());
				sb.append(color.getDzColorCode() + "-");
			}
			if(osc.getOrderProductId() != null){
				OrderProduct op = (OrderProduct)dao.get(OrderProduct.class.getName(), osc.getOrderProductId());
				sb.append(op.getOrderNumber() + "-");
			}
			if("01".equals(osc.getIsTrimmingNeeded())){
				sb.append(i18n.getShortMessage(locale, "ui.order.istrimmingneeded"));
			}else{
				sb.append(i18n.getShortMessage(locale, "ui.order.isyarnneeded"));
			}
			return sb.toString();
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
	}
	
	public static String pdfFileName(){
		
		return null;
	}

}
